package ims.servlet;

import ims.beans.ProductType;
import ims.constants.IMSConstants;
import ims.db.add.prodType.AddProdTypeDAO;
import ims.db.add.prodType.AddProdTypeDbRequest;
import ims.db.add.prodType.AddProdTypeDbResponse;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class AddProdTypeServlet
 */
@WebServlet("/AddProdTypeServlet")
public class AddProdTypeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddProdTypeServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try
		{
			String newProdType = request.getParameter("prodType");
			
			response.setContentType("text/html;charset=UTF-8");
	
			//Retrieve all product types
			AddProdTypeDbRequest dbRequest = new AddProdTypeDbRequest();
			ProductType prodType = new ProductType();
			prodType.setProdType(newProdType);
			prodType.setProdTypeId("PT00"+IMSConstants.PROD_TYPE_ID);
			dbRequest.setProdType(prodType);
			AddProdTypeDAO db = new AddProdTypeDAO();
			AddProdTypeDbResponse dbResponse = (AddProdTypeDbResponse) db.execute(dbRequest);
			
			String nextLoc = "/IMS/prodType?"+IMSConstants.RESPONSE_HEADER+"="+dbResponse.getMsg();      
			response.sendRedirect(response.encodeRedirectURL(nextLoc));
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
       
	}

}
